package com.oros.db;

import java.util.Collection;

import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Container.Filterable;
import com.vaadin.data.Container.Indexed;
import com.vaadin.data.Container.ItemSetChangeNotifier;
import com.vaadin.data.Container.Ordered;
import com.vaadin.data.Container.Sortable;
import com.vaadin.data.util.filter.UnsupportedFilterException;
import java.util.ArrayList;
public class AESubContainer implements Container, Indexed,
		ItemSetChangeNotifier, Ordered, Filterable,Sortable {

	private ArrayList<String> columnNameCollection=null;
	public AETableContainer base;
	private boolean isNewFilteredOut;
	private static final long serialVersionUID = -6078440159512806838L;
    
	@SuppressWarnings("unchecked")
	public AESubContainer(AETableContainer base,boolean isNewFilteredOut,String[] columns)
	{
		this.base=base;
		this.isNewFilteredOut = isNewFilteredOut;
		if (columns!=null && columns.length>0)
		{
			int n=columns.length;
			columnNameCollection = new ArrayList<String>(n);
			for(int i=0;i<n;i++)
			{
				if (base.getColumnIndex(columns[i])<0)
					throw new RuntimeException("AESubContainer: Column "+columns[i]+" is not present in the base table");
				columnNameCollection.add(columns[i]);
			}
		}
		else columnNameCollection =(ArrayList<String>) base.getContainerPropertyIds();
	}
	@Override
	public void addContainerFilter(Filter filter)
			throws UnsupportedFilterException {
         base.addContainerFilter(filter);
	}

	@Override
	public void removeContainerFilter(Filter filter) {
		base.removeContainerFilter(filter);
	}

	@Override
	public void removeAllContainerFilters() {
        base.removeAllContainerFilters();
	}

	@Override
	public Object nextItemId(Object itemId) {
		Object a = base.nextItemId(itemId);
		int k = base.indexOfId(a);
		int n = size();
		if (k>=n) return null;
		return a; 
	}

	@Override
	public Object prevItemId(Object itemId) {
		return base.prevItemId(itemId);
	}

	@Override
	public Object firstItemId() {
		if (size()==0) return null;
		return base.firstItemId();
	}

	@Override
	public Object lastItemId() {
		int n=size();
		if (n==0) return null;
		return getIdByIndex(n-1);
	}

	@Override
	public boolean isFirstId(Object itemId) {
		return size()>0 && base.isFirstId(itemId);
	}

	@Override
	public boolean isLastId(Object itemId) {
		int n=size();
		int k=indexOfId(itemId);
		return (n>0) && (k==n-1);
	}

	@Override
	public Object addItemAfter(Object previousItemId)
			throws UnsupportedOperationException {
		return base.addItemAfter(previousItemId);
	}

	@Override
	public Item addItemAfter(Object previousItemId, Object newItemId)
			throws UnsupportedOperationException {
		return base.addItemAfter(previousItemId, newItemId);
	}

	@Override
	public void addListener(ItemSetChangeListener listener) {
         base.addListener(listener);
	}

	@Override
	public void removeListener(ItemSetChangeListener listener) {
		base.removeListener(listener);
	}

	@Override
	public int indexOfId(Object itemId) {
		return base.indexOfId(itemId);
	}

	@Override
	public Object getIdByIndex(int index) {
		return base.getIdByIndex(index);
	}

	@Override
	public Object addItemAt(int index) throws UnsupportedOperationException {
		return base.addItemAt(index);
	}

	@Override
	public Item addItemAt(int index, Object newItemId)
			throws UnsupportedOperationException {
		return base.addItemAt(index, newItemId);
	}

	@Override
	public Item getItem(Object itemId) {
		return base.getItem(itemId);
	}

	@Override
	public Collection<?> getContainerPropertyIds() {
		return columnNameCollection;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<?> getItemIds() {
		Collection<?> a=base.getItemIds();
		int n= size();
		if (n!=a.size())
		{
			ArrayList<Object> b = new ArrayList<Object>(n);
	        ArrayList<Object> c = (ArrayList<Object>)a;
	        for(int i=0;i<n;i++)
	        {
	        	b.add(c.get(i));
	        }
	        a=b;
		}
		return a;
	}

	@Override
	public Property getContainerProperty(Object itemId, Object propertyId) {
		return base.getContainerProperty(itemId, propertyId);
	}

	@Override
	public Class<?> getType(Object propertyId) {
		return base.getType(propertyId);
	}

	@Override
	public int size() {
        int n=base.size();
        if (isNewFilteredOut)
        {
           while (n>0)
           {
        	 if (base.isItemNew(n-1)) n--;
        	 else break;
           }
        }
 		return n; 
	}

	@Override
	public boolean containsId(Object itemId) {
		return base.containsId(itemId);
	}

	@Override
	public Item addItem(Object itemId) throws UnsupportedOperationException {
		return base.addItem(itemId);
	}

	@Override
	public Object addItem() throws UnsupportedOperationException {
		return base.addItem();
	}

	@Override
	public boolean removeItem(Object itemId)
			throws UnsupportedOperationException {
		return base.removeItem(itemId);
	}

	@Override
	public boolean addContainerProperty(Object propertyId, Class<?> type,
			Object defaultValue) throws UnsupportedOperationException {
		throw new UnsupportedOperationException("addContainerProperty");
	}

	@Override
	public boolean removeContainerProperty(Object propertyId)
			throws UnsupportedOperationException {
		throw new UnsupportedOperationException("removeContainerProperty");
	}

	@Override
	public boolean removeAllItems() throws UnsupportedOperationException {
		return base.removeAllItems();
	}
	@Override
	public void sort(Object[] propertyId, boolean[] ascending) {
		base.sort(propertyId, ascending);
	}
	@Override
	public Collection<?> getSortableContainerPropertyIds() {
		return base.getSortableContainerPropertyIds();
	}

}
